from django.core.paginator import Paginator, EmptyPage
from django.views import View
from goods.models import GoodsCategory, SKU
from django.http import JsonResponse
from goods.utils import get_breadcrumb


# 商品列表页
class ListView(View):
    def get(self, request, category_id):

        # 获取参数:
        page = request.GET.get('page')
        page_size = request.GET.get('page_size')
        ordering = request.GET.get('ordering')

        # 判断category_id是否正确
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except Exception as e:
            return JsonResponse({'code': 400, 'errmsg': '获取mysql数据出错'})

        # 查询面包屑导航(函数在下面写着)
        breadcrumb = get_breadcrumb(category)

        # 通过category_id获取商品并排序
        try:
            skus = SKU.objects.filter(category=category, is_launched=True).order_by(ordering)
        except Exception as e:
            return JsonResponse({'code': 400, 'errmsg': '获取mysql数据出错'})

        # 创建分页
        paginator = Paginator(skus, page_size)

        # 获取每页数据
        try:
            page_skus = paginator.page(page)
        except EmptyPage:
            return JsonResponse({'code': 400, 'errmsg': 'page数据出错'})

        # 获取列表页总页数
        total_page = paginator.num_pages

        # 整理数据
        data_list = []

        # 通过category_id获取商品并排序, 并取前两个数据
        for sku in page_skus:
            data_list.append({
                'id': sku.id,
                'default_image_url': sku.default_image_url,
                'name': sku.name,
                'price': sku.price
            })

        return JsonResponse({'code': 0, 'errmsg': 'ok', 'breadcrumb': breadcrumb,
                             'list': data_list, 'count': total_page})


# 商品热销排行
class HotGoodsView(View):
    def get(self, request, category_id):
        try:
            skus = SKU.objects.filter(category_id=category_id, is_launched=True).order_by('-sales')[:2]
        except Exception as e:
            return JsonResponse({'code': 400, 'errmsg': '获取商品数据出错'})

        # 整理数据
        hot_skus = []
        for sku in skus:
            hot_skus.append({
                'id': sku.id,
                'default_image_url': sku.default_image_url,
                'name': sku.name,
                'price': sku.price
            })

        return JsonResponse({'code': 0, 'errmsg': 'ok', 'hot_skus': hot_skus})


from haystack.views import SearchView


class MySearchView(SearchView):
    '''重写SearchView类'''
    def create_response(self):
        page = self.request.GET.get('page')
        # 获取搜索结果
        context = self.get_context()
        data_list = []
        for sku in context['page'].object_list:
            data_list.append({
                'id':sku.object.id,
                'name':sku.object.name,
                'price':sku.object.price,
                'default_image_url':sku.object.default_image_url,
                'searchkey':context.get('query'),
                'page_size':context['page'].paginator.num_pages,
                'count':context['page'].paginator.count
            })
        # 拼接参数, 返回
        return JsonResponse(data_list, safe=False)
